package com.kefu.chat.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.chat.model.ChatMenuConfig;
import com.kefu.chat.model.ChatMenuConfigPlan;
import com.kefu.chat.service.ChatMenuConfigPlanService;
import com.kefu.chat.service.ChatMenuConfigService;
import com.kefu.chat.vo.*;
import com.kefu.common.context.KefuContext;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.util.Assert;
import com.kefu.common.vo.BatchVo;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api(tags="快捷菜单/方案")
@RestController
@RequestMapping("/chat/menu/")
public class ChatMenuController extends AbstractBaseController{
    @Resource
    private ChatMenuConfigService chatMenuConfigService;
    @Resource
    private ChatMenuConfigPlanService chatMenuConfigPlanService;

    @PostMapping("add")
    @ApiOperation(value="添加快捷菜单", notes="添加快捷菜单")
    public Result<String> add(@Validated @RequestBody ChatMenuConfigInsertVo chatMenuVo) {
        logger.info("添加快捷菜单:{}",chatMenuVo);

        long count = chatMenuConfigService.count(
                Wrappers.lambdaQuery(ChatMenuConfig.class).eq(ChatMenuConfig::getTenantId, KefuContext.getTid())
                .eq(ChatMenuConfig::getMenuName, chatMenuVo.getMenuName()));
        Assert.isTrue(count > 0, "菜单名[" + chatMenuVo.getMenuName() + "]已经存在！");
        ChatMenuConfig info = chatMenuVo.convert();
        chatMenuConfigService.save(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    @PostMapping("update")
    @ApiOperation(value="修改快捷菜单", notes="修改快捷菜单")
    public Result<String> update(@Validated @RequestBody ChatMenuConfigUpdateVo chatMenuVo) {
        logger.info("修改快捷菜单:{}",chatMenuVo);

        List<ChatMenuConfig> list = chatMenuConfigService.list(Wrappers.lambdaQuery(ChatMenuConfig.class)
                .eq(ChatMenuConfig::getTenantId, KefuContext.getTid()).eq(ChatMenuConfig::getMenuName, chatMenuVo.getMenuName()));
        if (CollUtil.isNotEmpty(list)) {
            boolean flag = true;
            for (ChatMenuConfig config :
                    list) {
                if (!config.getId().equals(chatMenuVo.getId())) {
                    flag = false;
                    break;
                }
            }
            Assert.isTrue(!flag, "菜单名[" + chatMenuVo.getMenuName() + "]已经存在！");
        }
        ChatMenuConfig info = chatMenuVo.convert();
        chatMenuConfigService.updateById(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    @PostMapping("batchDelete")
    @ApiOperation(value="批量删除快捷菜单", notes="批量删除快捷菜单")
    public Result<String> batchDelete(@Validated @RequestBody BatchVo batch) {
        logger.info("删除快捷菜单:{}",batch);

        Assert.isEmpty(batch.getIds(), "请选择要删除的快捷菜单！");
        boolean flag = true;
        List<String> ids = batch.getIds();
        for (String id : ids) {
            long count = chatMenuConfigPlanService.count(Wrappers.lambdaQuery(ChatMenuConfigPlan.class)
                    .like(ChatMenuConfigPlan::getMenus, id));
            if (count > 0) {
                flag = false;
                break;
            }
        }
        Assert.isTrue(!flag, "快捷菜单已经被引用，不能删除！");
        chatMenuConfigService.batchDelete(batch.getIds());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("delete")
    @ApiOperation(value="删除快捷菜单", notes="删除快捷菜单")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除快捷菜单:{}",delete);

        long count = chatMenuConfigPlanService.count(Wrappers.lambdaQuery(ChatMenuConfigPlan.class)
                .like(ChatMenuConfigPlan::getMenus, delete.getId()));
        Assert.isTrue(count > 0, "菜单已经被使用，不能删除！");
        chatMenuConfigService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult(delete.getId());
    }

    @PostMapping("list")
    @ApiOperation(value="查询快捷菜单", notes="查询快捷菜单")
    public Result<Page<ChatMenuConfig>> list(@Validated @RequestBody ChatMenuConfigSearchVo search) {
        logger.info("查询快捷菜单:{}",search);

        Page<ChatMenuConfig> page = chatMenuConfigService.searchLambdaPage(search);
        return ResultGenerator.genSuccessResult(page);
    }

    @PostMapping("all")
    @ApiOperation(value="查询快捷菜单", notes="查询快捷菜单")
    public Result<List<ChatMenuConfig>> all(@Validated @RequestBody ChatMenuConfigSearchVo search) {
        logger.info("查询快捷菜单:{}",search);

        List<ChatMenuConfig> page = chatMenuConfigService.getAll(search);
        return ResultGenerator.genSuccessResult(page);
    }

    @PostMapping("addPlan")
    @ApiOperation(value="添加快捷方案", notes="添加快捷方案")
    public Result<String> add(@Validated @RequestBody ChatMenuConfigPlanInsertVo chatMenuPlanVo) {
        logger.info("添加快捷方案:{}",chatMenuPlanVo);

        long count = chatMenuConfigPlanService.count(Wrappers.lambdaQuery(ChatMenuConfigPlan.class)
                .eq(ChatMenuConfigPlan::getTenantId, KefuContext.getTid()).eq(ChatMenuConfigPlan::getPlanName, chatMenuPlanVo.getPlanName()));
        Assert.isTrue(count > 0, "快捷方案名[" + chatMenuPlanVo.getPlanName() + "]已经存在！");
        ChatMenuConfigPlan info = chatMenuPlanVo.convert();
        chatMenuConfigPlanService.save(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    @PostMapping("updatePlan")
    @ApiOperation(value="修改快捷方案", notes="修改快捷方案")
    public Result<String> add(@Validated @RequestBody ChatMenuConfigPlanUpdateVo chatMenuPlanVo) {
        logger.info("修改快捷方案:{}",chatMenuPlanVo);

        List<ChatMenuConfigPlan> list = chatMenuConfigPlanService.list(Wrappers.lambdaQuery(ChatMenuConfigPlan.class).eq(ChatMenuConfigPlan::getTenantId, KefuContext.getTid())
                .eq(ChatMenuConfigPlan::getPlanName, chatMenuPlanVo.getPlanName()));
        if (CollUtil.isNotEmpty(list)) {
            boolean flag = true;
            for (ChatMenuConfigPlan plan :
                    list) {
                if (!plan.getId().equals(chatMenuPlanVo.getId())) {
                    flag = false;
                    break;
                }
            }
            Assert.isTrue(!flag, "方案名[" + chatMenuPlanVo.getPlanName() + "]已经存在！");
        }
        ChatMenuConfigPlan info = chatMenuPlanVo.convert();
        chatMenuConfigPlanService.update(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    @PostMapping("detailPlan")
    @ApiOperation(value="查询方案详情", notes="查询方案详情")
    public Result<ChatMenuConfigPlan> detail(@Validated @RequestBody DetailVo detail) {
        logger.info("查询方案详情:{}",detail);

        ChatMenuConfigPlan info = chatMenuConfigPlanService.getById(detail.getId());
        return ResultGenerator.genSuccessResult(info);
    }

    @PostMapping("batchDeletePlan")
    @ApiOperation(value="批量删除快捷方案", notes="批量删除快捷方案")
    public Result<String> batchDeletePlan(@Validated @RequestBody BatchVo batch) {
        logger.info("批量删除快捷方案:{}",batch);

        chatMenuConfigPlanService.batchDeletePlan(batch.getIds());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("deletePlan")
    @ApiOperation(value="删除快捷方案", notes="删除快捷方案")
    public Result<String> deletePlan(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除快捷方案:{}",delete);

        chatMenuConfigPlanService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult(delete.getId());
    }

    @PostMapping("listPlan")
    @ApiOperation(value="查询快捷方案", notes="查询快捷方案")
    public Result<Page<ChatMenuConfigPlan>> list(@Validated @RequestBody ChatMenuConfigPlanSearchVo search) {

        Page<ChatMenuConfigPlan> page = chatMenuConfigPlanService.listPlan(search);
        return ResultGenerator.genSuccessResult(page);
    }
}
